package cn.iocoder.yudao.module.infra.util;

import cn.hutool.core.util.StrUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.MDC;

public class GameMatchIdUtil {
    private static final String TRANSACTION_ID_KEY = "mid";

    public static String setMatchId() {
        return setMatchId(null);
    }

    public static String setMatchId(String mid) {
        MDC.put(TRANSACTION_ID_KEY, mid);
        return mid;
    }

    public static String getMatchId() {
        final String mid = MDC.get(TRANSACTION_ID_KEY);
        return mid;
    }

    public static void removeMatchId() {
        MDC.remove(TRANSACTION_ID_KEY);
    }


    public static void setMatchId(HttpServletRequest request, String traceId) {
        GameMatchIdUtil.setMatchId(traceId);
        request.setAttribute(TRANSACTION_ID_KEY, traceId);
    }

    public static String getMatchId(HttpServletRequest request) {
        String traceId = (String) request.getAttribute(TRANSACTION_ID_KEY);
        if (StrUtil.isEmpty(traceId)) {
            traceId = getMatchId();
        }
        return traceId;
    }

    public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE);
    }

}
